/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

package com.xrkol.sharelib.config;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

@WebFilter(filterName = "urlFilter" , urlPatterns = { "/*" })
public class UrlFilter extends OncePerRequestFilter{
    
    private static final String DEFAULT_URI_ENCODING="UTF-8";
    
    @Override
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
            throws ServletException, IOException {
        String uri=req.getRequestURI();
        String decodeUri=URLDecoder.decode(uri, DEFAULT_URI_ENCODING);
        if(uri.equals(decodeUri)){
            chain.doFilter(req, res);
        }else{
            String requestUri=decodeUri.substring(req.getContextPath().length());
            req.getServletContext().getRequestDispatcher(requestUri).forward(req, res);
        }
    }

}
 